package com.study.experiment05.bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


/**
 * @author WS
 */
public class UserList {
    private DBConn conn;
    private String sql;

    public UserList() {
        conn = new DBConn();
        sql = "select * from tab_user";
    }

    public List<User> list() {
        List<User> users = new ArrayList<>();
        Connection connection = conn.getConn();
        Statement st;
        ResultSet rs = null;
        try {
            st = connection.createStatement();
            rs = st.executeQuery(sql);
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("name"));
                user.setPassword(rs.getString("pwd"));
                users.add(user);
            }
            return users;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
